package xyj.sparrow.common.netty.encoder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import xyj.sparrow.common.netty.util.ProtostuffUtil;

/**
 * @ClassName ProtostuffEncoder
 * @Description TODO
 * @Author Jim
 * @Date 2021/1/15 21:12
 * @Version 1.0.0
 **/
public class ProtostuffEncoder extends MessageToByteEncoder<Object> {
    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
        byte[] serialize = ProtostuffUtil.serialize(msg);
        out.writeInt(serialize.length);
        out.writeBytes(serialize);
    }
}
